using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Reflection;
using ASPJPEGLib;


/// <summary>
/// Summary description for bzImage
/// </summary>
public class bzImage
{
    public static bool resizeImage(string sFilePath, string sSavePathThumb, string sSavePathFull, int iWidthThumb, int iHeightThumb, int iWidthFull, int iHeightFull, bool gifFile)
    {
        if (iWidthThumb == 0 && iHeightThumb == 0)
        {
            try
            {
                iWidthThumb = Convert.ToInt32(ConfigurationManager.AppSettings["ImageMinSize"]);
            }
            catch
            {
                iWidthThumb = 150;
            }

        }
        if (iWidthFull == 0 && iHeightFull == 0)
        {
            try
            {
                iWidthFull = Convert.ToInt32(ConfigurationManager.AppSettings["ImageMaxSize"]);
            }
            catch
            {
                iWidthFull = 600;
            }

        }
        bool flg = true;
        if (gifFile)
        {
            if (!bzImage.resizeImage_Gif(sFilePath, sSavePathThumb, iWidthThumb, iHeightThumb)) flg = false;
            if (!bzImage.resizeImage_Gif(sFilePath, sSavePathFull, iWidthFull, iHeightFull)) flg = false;
                
        }
        else
        {

            if (!bzImage.resizeImage_NoGif(sFilePath, sSavePathThumb, iWidthThumb, iHeightThumb)) flg = false;
            if (!bzImage.resizeImage_NoGif(sFilePath, sSavePathFull, iWidthFull, iHeightFull)) flg = false;
               
        }        
        return flg;
    }
    public static bool resizeImage_NoGif(string sFileName, string sSavePath, int iWidth, int iHeight)
    {
        try
        {
            bool bResize = Convert.ToInt16(ConfigurationManager.AppSettings["ImageResize"]) == 1;
            ASPJpeg oJPG = new ASPJpeg();
            oJPG.Open(sFileName);
            oJPG.Quality = 100;
            oJPG.PreserveAspectRatio = 0;
            if (iWidth <= 0 || iHeight <= 0) oJPG.PreserveAspectRatio = 1;
            if (iWidth == 0 && iHeight == 0 && (bResize || oJPG.Width > 200)) oJPG.Width = 200;
            if (iWidth > 0 && (bResize || oJPG.Width > iWidth)) oJPG.Width = iWidth;
            if (iHeight > 0 && (bResize || oJPG.Height > iHeight)) oJPG.Height = iHeight;
            oJPG.Save(sSavePath);
            return true;
        }
        catch { return false; }
    }

    public static bool resizeImage_Gif(string sFileName, string sSavePath, int iWidth, int iHeight)
    {
        try
        {
            bool bResize = Convert.ToInt16(ConfigurationManager.AppSettings["ImageResize"]) == 1;
            ASPJpeg oJPG = new ASPJpeg();
            IGif oGIF = oJPG.Gif;
            oJPG.Gif.Open(sFileName);
            if (bResize)
            {
                if (iWidth > 0 && iHeight > 0) oJPG.Gif.Resize(iWidth, iHeight, Missing.Value);
                else if (iWidth > 0 && iHeight <= 0) oJPG.Gif.Resize(iWidth, Missing.Value, Missing.Value);
                else if (iWidth <= 0 && iHeight > 0) oJPG.Gif.Resize(Missing.Value, iHeight, Missing.Value);
                else if (iWidth == 0 && iWidth == 0) oJPG.Gif.Resize(200, Missing.Value, Missing.Value);
            }
            else
            {
                if (iWidth > 0 && iHeight > 0 && oJPG.Gif.Width > iWidth) oJPG.Gif.Resize(iWidth, iHeight, Missing.Value);
                else if (iWidth > 0 && iHeight <= 0 && oJPG.Gif.Width > iWidth) oJPG.Gif.Resize(iWidth, Missing.Value, Missing.Value);
                else if (iWidth <= 0 && iHeight > 0 && oJPG.Gif.Height > iHeight) oJPG.Gif.Resize(Missing.Value, iHeight, Missing.Value);
                else if (iWidth == 0 && iWidth == 0 && oJPG.Gif.Width > 200) oJPG.Gif.Resize(200, Missing.Value, Missing.Value);
            }
            oGIF.Save(sSavePath);
            return true;
        }
        catch { return false; }
    }
}
